<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>xml.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_xml.js.html">xml.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview
<a name="line17"></a> * XML utilities.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.dom.xml&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.dom&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>/**
<a name="line28"></a> * Max XML size for MSXML2.  Used to prevent potential DoS attacks.
<a name="line29"></a> * @type {number}
<a name="line30"></a> */
<a name="line31"></a>goog.dom.xml.MAX_XML_SIZE_KB = 2 * 1024;  // In kB
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * Max XML size for MSXML2.  Used to prevent potential DoS attacks.
<a name="line36"></a> * @type {number}
<a name="line37"></a> */
<a name="line38"></a>goog.dom.xml.MAX_ELEMENT_DEPTH = 256; // Same default as MSXML6.
<a name="line39"></a>
<a name="line40"></a>
<a name="line41"></a>/**
<a name="line42"></a> * Creates an XML document appropriate for the current JS runtime
<a name="line43"></a> * @param {string=} opt_rootTagName The root tag name.
<a name="line44"></a> * @param {string=} opt_namespaceUri Namespace URI of the document element.
<a name="line45"></a> * @return {Document} The new document.
<a name="line46"></a> */
<a name="line47"></a>goog.dom.xml.createDocument = function(opt_rootTagName, opt_namespaceUri) {
<a name="line48"></a>  if (opt_namespaceUri &amp;&amp; !opt_rootTagName) {
<a name="line49"></a>    throw Error(&quot;Can&#39;t create document with namespace and no root tag&quot;);
<a name="line50"></a>  }
<a name="line51"></a>  if (document.implementation &amp;&amp; document.implementation.createDocument) {
<a name="line52"></a>    return document.implementation.createDocument(opt_namespaceUri || &#39;&#39;,
<a name="line53"></a>                                                  opt_rootTagName || &#39;&#39;,
<a name="line54"></a>                                                  null);
<a name="line55"></a>  } else if (typeof ActiveXObject != &#39;undefined&#39;) {
<a name="line56"></a>    var doc = goog.dom.xml.createMsXmlDocument_();
<a name="line57"></a>    if (doc) {
<a name="line58"></a>      if (opt_rootTagName) {
<a name="line59"></a>        doc.appendChild(doc.createNode(goog.dom.NodeType.ELEMENT,
<a name="line60"></a>                                       opt_rootTagName,
<a name="line61"></a>                                       opt_namespaceUri || &#39;&#39;));
<a name="line62"></a>      }
<a name="line63"></a>      return doc;
<a name="line64"></a>    }
<a name="line65"></a>  }
<a name="line66"></a>  throw Error(&#39;Your browser does not support creating new documents&#39;);
<a name="line67"></a>};
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * Creates an XML document from a string
<a name="line72"></a> * @param {string} xml The text.
<a name="line73"></a> * @return {Document} XML document from the text.
<a name="line74"></a> */
<a name="line75"></a>goog.dom.xml.loadXml = function(xml) {
<a name="line76"></a>  if (typeof DOMParser != &#39;undefined&#39;) {
<a name="line77"></a>    return new DOMParser().parseFromString(xml, &#39;application/xml&#39;);
<a name="line78"></a>  } else if (typeof ActiveXObject != &#39;undefined&#39;) {
<a name="line79"></a>    var doc = goog.dom.xml.createMsXmlDocument_();
<a name="line80"></a>    doc.loadXML(xml);
<a name="line81"></a>    return doc;
<a name="line82"></a>  }
<a name="line83"></a>  throw Error(&#39;Your browser does not support loading xml documents&#39;);
<a name="line84"></a>};
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Serializes an XML document or subtree to string.
<a name="line89"></a> * @param {Document|Element} xml The document or the root node of the subtree.
<a name="line90"></a> * @return {string} The serialized XML.
<a name="line91"></a> */
<a name="line92"></a>goog.dom.xml.serialize = function(xml) {
<a name="line93"></a>  // Compatible with Firefox, Opera and WebKit.
<a name="line94"></a>  if (typeof XMLSerializer != &#39;undefined&#39;) {
<a name="line95"></a>    return new XMLSerializer().serializeToString(xml);
<a name="line96"></a>  }
<a name="line97"></a>  // Compatible with Internet Explorer.
<a name="line98"></a>  var text = xml.xml;
<a name="line99"></a>  if (text) {
<a name="line100"></a>    return text;
<a name="line101"></a>  }
<a name="line102"></a>  throw Error(&#39;Your browser does not support serializing XML documents&#39;);
<a name="line103"></a>};
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Selects a single node using an Xpath expression and a root node
<a name="line108"></a> * @param {Node} node The root node.
<a name="line109"></a> * @param {string} path Xpath selector.
<a name="line110"></a> * @return {Node} The selected node, or null if no matching node.
<a name="line111"></a> */
<a name="line112"></a>goog.dom.xml.selectSingleNode = function(node, path) {
<a name="line113"></a>  if (typeof node.selectSingleNode != &#39;undefined&#39;) {
<a name="line114"></a>    var doc = goog.dom.getOwnerDocument(node);
<a name="line115"></a>    if (typeof doc.setProperty != &#39;undefined&#39;) {
<a name="line116"></a>      doc.setProperty(&#39;SelectionLanguage&#39;, &#39;XPath&#39;);
<a name="line117"></a>    }
<a name="line118"></a>    return node.selectSingleNode(path);
<a name="line119"></a>  } else if (document.implementation.hasFeature(&#39;XPath&#39;, &#39;3.0&#39;)) {
<a name="line120"></a>    var doc = goog.dom.getOwnerDocument(node);
<a name="line121"></a>    var resolver = doc.createNSResolver(doc.documentElement);
<a name="line122"></a>    var result = doc.evaluate(path, node, resolver,
<a name="line123"></a>        XPathResult.FIRST_ORDERED_NODE_TYPE, null);
<a name="line124"></a>    return result.singleNodeValue;
<a name="line125"></a>  }
<a name="line126"></a>  return null;
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * Selects multiple nodes using an Xpath expression and a root node
<a name="line132"></a> * @param {Node} node The root node.
<a name="line133"></a> * @param {string} path Xpath selector.
<a name="line134"></a> * @return {(NodeList,Array.&lt;Node&gt;)} The selected nodes, or empty array if no
<a name="line135"></a> *     matching nodes.
<a name="line136"></a> */
<a name="line137"></a>goog.dom.xml.selectNodes = function(node, path) {
<a name="line138"></a>  if (typeof node.selectNodes != &#39;undefined&#39;) {
<a name="line139"></a>    var doc = goog.dom.getOwnerDocument(node);
<a name="line140"></a>    if (typeof doc.setProperty != &#39;undefined&#39;) {
<a name="line141"></a>      doc.setProperty(&#39;SelectionLanguage&#39;, &#39;XPath&#39;);
<a name="line142"></a>    }
<a name="line143"></a>    return node.selectNodes(path);
<a name="line144"></a>  } else if (document.implementation.hasFeature(&#39;XPath&#39;, &#39;3.0&#39;)) {
<a name="line145"></a>    var doc = goog.dom.getOwnerDocument(node);
<a name="line146"></a>    var resolver = doc.createNSResolver(doc.documentElement);
<a name="line147"></a>    var nodes = doc.evaluate(path, node, resolver,
<a name="line148"></a>        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
<a name="line149"></a>    var results = [];
<a name="line150"></a>    var count = nodes.snapshotLength;
<a name="line151"></a>    for (var i = 0; i &lt; count; i++) {
<a name="line152"></a>      results.push(nodes.snapshotItem(i));
<a name="line153"></a>    }
<a name="line154"></a>    return results;
<a name="line155"></a>  } else {
<a name="line156"></a>    return [];
<a name="line157"></a>  }
<a name="line158"></a>};
<a name="line159"></a>
<a name="line160"></a>
<a name="line161"></a>/**
<a name="line162"></a> * Sets multiple attributes on an element. Differs from goog.dom.setProperties
<a name="line163"></a> * in that it exclusively uses the element&#39;s setAttributes method. Use this
<a name="line164"></a> * when you need to ensure that the exact property is available as an attribute
<a name="line165"></a> * and can be read later by the native getAttribute method.
<a name="line166"></a> * @param {!Element} element XML or DOM element to set attributes on.
<a name="line167"></a> * @param {!Object.&lt;string, string&gt;} attributes Map of property:value pairs.
<a name="line168"></a> */
<a name="line169"></a>goog.dom.xml.setAttributes = function(element, attributes) {
<a name="line170"></a>  for (var key in attributes) {
<a name="line171"></a>    if (attributes.hasOwnProperty(key)) {
<a name="line172"></a>      element.setAttribute(key, attributes[key]);
<a name="line173"></a>    }
<a name="line174"></a>  }
<a name="line175"></a>};
<a name="line176"></a>
<a name="line177"></a>
<a name="line178"></a>/**
<a name="line179"></a> * Creates an instance of the MSXML2.DOMDocument.
<a name="line180"></a> * @return {Document} The new document.
<a name="line181"></a> * @private
<a name="line182"></a> */
<a name="line183"></a>goog.dom.xml.createMsXmlDocument_ = function() {
<a name="line184"></a>  var doc = new ActiveXObject(&#39;MSXML2.DOMDocument&#39;);
<a name="line185"></a>  if (doc) {
<a name="line186"></a>    // Prevent potential vulnerabilities exposed by MSXML2, see
<a name="line187"></a>    // http://b/1707300 and http://wiki/Main/ISETeamXMLAttacks for details.
<a name="line188"></a>    doc.resolveExternals = false;
<a name="line189"></a>    doc.validateOnParse = false;
<a name="line190"></a>    // Add a try catch block because accessing these properties will throw an
<a name="line191"></a>    // error on unsupported MSXML versions. This affects Windows machines
<a name="line192"></a>    // running IE6 or IE7 that are on XP SP2 or earlier without MSXML updates.
<a name="line193"></a>    // See http://msdn.microsoft.com/en-us/library/ms766391(VS.85).aspx for
<a name="line194"></a>    // specific details on which MSXML versions support these properties.
<a name="line195"></a>    try {
<a name="line196"></a>      doc.setProperty(&#39;ProhibitDTD&#39;, true);
<a name="line197"></a>      doc.setProperty(&#39;MaxXMLSize&#39;, goog.dom.xml.MAX_XML_SIZE_KB);
<a name="line198"></a>      doc.setProperty(&#39;MaxElementDepth&#39;, goog.dom.xml.MAX_ELEMENT_DEPTH);
<a name="line199"></a>    } catch (e) {
<a name="line200"></a>      // No-op.
<a name="line201"></a>    }
<a name="line202"></a>  }
<a name="line203"></a>  return doc;
<a name="line204"></a>};
</pre>


</body>
</html>
